package com.ias.agdis_r.application;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

import com.ias.agdis_r.activity.StartActivity;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.util.sdcard.WriteLog;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 * Created by qibo.yu on 2015/9/21.
 */
public class AGDISApplication extends Application {

    public static final String PREF_UNCAUGHT_EXCEPTION_HAPPENED = "pref_uncaught_exception_happened";
    private List<Activity> activityList;

    @Override
    public void onCreate() {
        super.onCreate();
        activityList = Collections.synchronizedList(new ArrayList<Activity>());
        registerActivityLifecycleCallbacks(new SimpleActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                super.onActivityCreated(activity, bundle);
                activityList.add(activity);
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                super.onActivityDestroyed(activity);
                activityList.remove(activity);
            }
        });
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {

                Writer result = new StringWriter();
                PrintWriter printWriter = new PrintWriter(result);
                ex.printStackTrace(printWriter);
                String stacktrace = result.toString();
                WriteLog.getInstance().writeLog("catch exception:"+stacktrace);
                System.out.println("catch exception:"+stacktrace);
                if (ServiceManage.getInstance().getIsLogon()) {
                    SharedPreferences pres = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = pres.edit();
                    editor.putBoolean(PREF_UNCAUGHT_EXCEPTION_HAPPENED, true);
                    editor.commit();
                }
                reStartAPP(3000);
            }
        });
    }

    public void reStartAPP(int delay) {
        Intent intent = new Intent(this, StartActivity.class);
        PendingIntent restartIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, intent, 0);
        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + delay,
                restartIntent);
        exit();
        //android.os.Process.killProcess(android.os.Process.myPid());
    }

    private void exit() {
        Iterator<Activity> iterator = activityList.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            activity.finish();
            iterator.remove();
        }
        activityList.clear();
        System.exit(0);
    }
}
